/**
 * @Auther: ChenforCode
 * @Date: 2018/9/12 10:10
 * @Description: 完成与借书表相关的逻辑操作
 */
package cn.chenforcode.service;

import cn.chenforcode.dao.BookDAO;
import cn.chenforcode.dao.BorrowDAO;
import cn.chenforcode.dao.StudentDAO;
import cn.chenforcode.entity.Book;
import cn.chenforcode.entity.Borrow;
import cn.chenforcode.entity.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Service
public class BorrowService {

    @Autowired
    private BorrowDAO borrowDAO;

    @Autowired
    private StudentDAO studentDAO;

    @Autowired
    private BookDAO bookDAO;

    /**
     * @return java.lang.String
     * @Author ChenforCode
     * @Description //TODO 完成借书的相关逻辑操作
     * @Date 10:20 2018/9/12
     * @Param [sno, bno]
     **/
    public String borrowBook(String sno, String bno) {
        if (borrowDAO.queryCanBorrow(sno, bno) == 1) {
            //构建一个Borrow对象,并添加进借书记录
            Borrow borrow = new Borrow(sno, bno, new Timestamp(new Date().getTime()), null);
            borrowDAO.borrowBook(borrow);

            //修改用户信息, 已经拥有的数量加一
            Student student = studentDAO.getStudentBySno(sno);
            student.setHave(student.getHave() + 1);
            studentDAO.updateStudent(student);

            //修改书籍信息，剩余数量减一
            Book book = bookDAO.getBookByBno(bno);
            book.setStill(book.getStill() - 1);
            bookDAO.updateBook(book);

            return "success";
        }
        return "failed";
    }

    /**
     * @return java.lang.String
     * @Author ChenforCode
     * @Description //TODO 完成还书的主要逻辑
     * @Date 12:18 2018/9/12
     * @Param [sno, bno]
     **/
    public String returnBook(String sno, String bno) {
        int fine = 0;
        //拿到那条记录
        Borrow borrow = borrowDAO.getBorrowHaveNoReturn(sno, bno);

        //拿到借书时间将他加上两个月 变成约定归还日期
        Timestamp timestamp = borrow.getBordate();
        Calendar c1 = Calendar.getInstance();
        c1.setTime(timestamp);
        c1.add(Calendar.MONTH, 2);

        //约定的归还时间
        Timestamp timestamp1 = new Timestamp(c1.getTime().getTime());

        //真实的归还时间
        Timestamp timestamp2 = new Timestamp(new Date().getTime());

        fine = calFine(timestamp1, timestamp2);

        //在学生的账户中扣除罚金，并更新账户
        Student student = studentDAO.getStudentBySno(sno);
        student.setBalance(student.getBalance() - fine);
        studentDAO.updateStudent(student);

        //执行还书操作
        borrow.setRetdate(new Timestamp(new Date().getTime()));
        borrowDAO.returnBook(borrow);

        //修改学生信息和书籍信息
        Book book = bookDAO.getBookByBno(bno);
        book.setStill(book.getStill() + 1);
        bookDAO.updateBook(book);

        student.setHave(student.getHave() - 1);
        studentDAO.updateStudent(student);
        return "success";
    }

    /**
     * @return int
     * @Author ChenforCode
     * @Description //TODO 计算罚金 t1是约定归还时间 t2是真实归还时间
     * @Date 12:04 2018/9/12
     * @Param [t1, t2]
     **/
    public int calFine(Timestamp t1, Timestamp t2) {
        Calendar c1 = Calendar.getInstance();
        c1.setTime(t1);
        // 将时分秒,毫秒域清零
        c1.set(Calendar.HOUR_OF_DAY, 0);
        c1.set(Calendar.MINUTE, 0);
        c1.set(Calendar.SECOND, 0);
        c1.set(Calendar.MILLISECOND, 0);
        t1 = new Timestamp(c1.getTime().getTime());

        Calendar c2 = Calendar.getInstance();
        c2.setTime(t2);
        c2.set(Calendar.HOUR_OF_DAY, 0);
        c2.set(Calendar.MINUTE, 0);
        c2.set(Calendar.SECOND, 0);
        c2.set(Calendar.MILLISECOND, 0);
        t2 = new Timestamp(c2.getTime().getTime());

        //计算超时了多少天，并计算罚金，如果罚金为负数，说明不需要缴纳罚金，将罚金设置为0
        int days = (int) ((t2.getTime() - t1.getTime()) / (1000 * 3600 * 24));
        int fine = 5 * days;
        if (fine < 0) {
            return 0;
        } else {
            return fine;
        }
    }

    /**
     * @return java.util.List<cn.chenforcode.entity.Borrow>
     * @Author ChenforCode
     * @Description //TODO 得到某人还没有归还的书籍
     * @Date 8:59 2018/9/14
     * @Param [sno]
     **/
    public List<Book> getNoReturn(String sno) {
        List<Borrow> borrows = borrowDAO.getBorrowHaveNoReturn(sno);
        if (borrows == null) {
            return null;
        }
        //List初始化不能直接等于null，必须使用如下方法进行初始化
        List<Book> books = new ArrayList<>();
        for (Borrow b : borrows) {
            books.add(bookDAO.getBookByBno(b.getBno()));
        }

        return books;
    }
}
